﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using EazyCMS.DBUtility;
using EazyCMS.Common;

namespace EazyCMS.DAL
{
    /// <summary>
    /// 数据访问类:用户
    /// </summary>
    public partial class jyj_student
    {
        private string databaseprefix; //数据库表名前缀
        public jyj_student(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }

        #region 基本方法================================

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.student_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dt_student_info(");
            strSql.Append("user_name,user_sex,user_year,user_school,user_class,user_card,user_ticket)");
            strSql.Append(" values (");
            strSql.Append("@user_name,@user_sex,@user_year,@user_school,@user_class,@user_card,@user_ticket)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {					
					new SqlParameter("@user_name", SqlDbType.NVarChar,100),
					new SqlParameter("@user_sex", SqlDbType.NVarChar,50),
					new SqlParameter("@user_year", SqlDbType.NVarChar,100),
					new SqlParameter("@user_school", SqlDbType.NVarChar,100),
					new SqlParameter("@user_class", SqlDbType.NVarChar,50),
					new SqlParameter("@user_card", SqlDbType.NVarChar,255),
					new SqlParameter("@user_ticket", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.User_name;
            parameters[1].Value = model.User_sex;
            parameters[2].Value = model.User_year;
            parameters[3].Value = model.User_school;
            parameters[4].Value = model.User_class;
            parameters[5].Value = model.User_card;
            parameters[6].Value = model.User_ticket;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.student_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_student_info set ");
            strSql.Append("user_name=@user_name,");
            strSql.Append("user_sex=@user_sex,");
            strSql.Append("user_year=@user_year,");
            strSql.Append("user_school=@user_school,");
            strSql.Append("user_class=@user_class,");
            strSql.Append("user_card=@user_card,");
            strSql.Append("user_ticket=@user_ticket");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {					
					new SqlParameter("@user_name", SqlDbType.NVarChar,100),
					new SqlParameter("@user_sex", SqlDbType.NVarChar,20),
					new SqlParameter("@user_year", SqlDbType.NVarChar,100),
					new SqlParameter("@user_school", SqlDbType.NVarChar,50),
					new SqlParameter("@user_class", SqlDbType.NVarChar,50),
					new SqlParameter("@user_card", SqlDbType.NVarChar,150),
					new SqlParameter("@user_ticket", SqlDbType.NVarChar,150),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.User_name;
            parameters[1].Value = model.User_sex;
            parameters[2].Value = model.User_year;
            parameters[3].Value = model.User_school;
            parameters[4].Value = model.User_class;
            parameters[5].Value = model.User_card;
            parameters[6].Value = model.User_ticket;
            parameters[7].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int id)
        //{
        //    //删除用户主表
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("delete from dt_student_info ");
        //    strSql.Append(" where id=@id");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@id", SqlDbType.Int,4)};
        //    parameters[0].Value = id;
        //    List<CommandInfo> sqllist = new List<CommandInfo>();
        //    CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
        //    sqllist.Add(cmd);

        //    int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
        //    if (rowsAffected > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Addscore(Model.student_score model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dt_student_score(");
            strSql.Append("user_id,yuwen,shuxue,yingyu,huaxue,wuli,zhengzhi,lishi,tiyu,deyu,pingshichengji,add_time)");
            strSql.Append(" values (");
            strSql.Append("@user_id,@yuwen,@shuxue,@yingyu,@huaxue,@wuli,@zhengzhi,@lishi,@tiyu,@deyu,@pingshichengji,@add_time)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {					
					new SqlParameter("@yuwen", SqlDbType.NVarChar,100),
					new SqlParameter("@shuxue", SqlDbType.NVarChar,50),
					new SqlParameter("@yingyu", SqlDbType.NVarChar,100),
					new SqlParameter("@huaxue", SqlDbType.NVarChar,100),
					new SqlParameter("@wuli", SqlDbType.NVarChar,50),
					new SqlParameter("@zhengzhi", SqlDbType.NVarChar,255),
					new SqlParameter("@lishi", SqlDbType.NVarChar,100),
                    new SqlParameter("@tiyu", SqlDbType.NVarChar,100),
                    new SqlParameter("@deyu", SqlDbType.NVarChar,100),
                    new SqlParameter("@pingshichengji", SqlDbType.NVarChar,100),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                     new SqlParameter("@user_id", SqlDbType.Int)
                                        };
            parameters[0].Value = model.Yuwen;
            parameters[1].Value = model.Shuxue;
            parameters[2].Value = model.Yingyu;
            parameters[3].Value = model.Huaxue;
            parameters[4].Value = model.Wuli;
            parameters[5].Value = model.Zhengzhi;
            parameters[6].Value = model.Lishi;
            parameters[7].Value = model.Tiyu;
            parameters[8].Value = model.Deyu;
            parameters[9].Value = model.Pingshichengji;
            parameters[10].Value = model.Add_time;
            parameters[11].Value = model.User_id;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Updatescore(Model.student_score model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_student_score set ");
            strSql.Append("user_id=@user_id,");
            strSql.Append("yuwen=@yuwen,");
            strSql.Append("shuxue=@shuxue,");
            strSql.Append("yingyu=@yingyu,");
            strSql.Append("zhengzhi=@zhengzhi,");
            strSql.Append("wuli=@wuli,");
            strSql.Append("huaxue=@huaxue,");
            strSql.Append("tiyu=@tiyu,");
            strSql.Append("lishi=@lishi,");
            strSql.Append("deyu=@deyu,");
            strSql.Append("pingshichengji=@pingshichengji,");
            strSql.Append("add_time=@add_time");
            strSql.Append(" where id=@id");

            SqlParameter[] parameters = {					
					new SqlParameter("@yuwen", SqlDbType.NVarChar,100),
					new SqlParameter("@shuxue", SqlDbType.NVarChar,50),
					new SqlParameter("@yingyu", SqlDbType.NVarChar,100),
					new SqlParameter("@huaxue", SqlDbType.NVarChar,100),
					new SqlParameter("@wuli", SqlDbType.NVarChar,50),
					new SqlParameter("@zhengzhi", SqlDbType.NVarChar,255),
					new SqlParameter("@lishi", SqlDbType.NVarChar,100),
                    new SqlParameter("@tiyu", SqlDbType.NVarChar,100),
                    new SqlParameter("@deyu", SqlDbType.NVarChar,100),
                    new SqlParameter("@pingshichengji", SqlDbType.NVarChar,100),
                    new SqlParameter("@add_time", SqlDbType.DateTime),
                    new SqlParameter("@user_id", SqlDbType.Int),
                    new SqlParameter("@id", SqlDbType.Int,4)};

            parameters[0].Value = model.Yuwen;
            parameters[1].Value = model.Shuxue;
            parameters[2].Value = model.Yingyu;
            parameters[3].Value = model.Huaxue;
            parameters[4].Value = model.Wuli;
            parameters[5].Value = model.Zhengzhi;
            parameters[6].Value = model.Lishi;
            parameters[7].Value = model.Tiyu;
            parameters[8].Value = model.Deyu;
            parameters[9].Value = model.Pingshichengji;
            parameters[10].Value = model.Add_time;
            parameters[11].Value = model.User_id;
            parameters[12].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //删除用户主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_student_info ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Deletescore(int id)
        {
            //删除用户主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from dt_student_score ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.student_info GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,user_name,user_sex,user_year,user_school,user_class,user_card,user_ticket");
            strSql.Append(" from  dt_student_info");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.student_score GetscoreModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,yuwen,shuxue,yingyu,zhengzhi,wuli,huaxue,tiyu,lishi,deyu,pingshichengji,add_time,user_id");
            strSql.Append(" from  dt_student_score");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModelscore(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

       

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,user_name");
            strSql.Append(" FROM dt_student_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM dt_student_info");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetfenshuList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM dt_student_score");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        #endregion



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.student_info DataRowToModel(DataRow row)
        {
            Model.student_info model = new Model.student_info();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.Id = int.Parse(row["id"].ToString());
                }
                if (row["user_name"] != null && row["user_name"].ToString() != "")
                {
                    model.User_name = row["user_name"].ToString();
                }
                if (row["user_sex"] != null)
                {
                    model.User_sex = row["user_sex"].ToString();
                }
                if (row["user_year"] != null)
                {
                    model.User_year = row["user_year"].ToString();
                }
                if (row["user_school"] != null)
                {
                    model.User_school = row["user_school"].ToString();
                }
                if (row["user_class"] != null)
                {
                    model.User_class = row["user_class"].ToString();
                }
                if (row["user_card"] != null)
                {
                    model.User_card = row["user_card"].ToString();
                }
                if (row["user_ticket"] != null)
                {
                    model.User_ticket = row["user_ticket"].ToString();
                }
            }
            return model;
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.student_score DataRowToModelscore(DataRow row)
        {
            Model.student_score model = new Model.student_score();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.Id = int.Parse(row["id"].ToString());
                }
                if (row["yuwen"] != null && row["yuwen"].ToString() != "")
                {
                    model.Yuwen = row["yuwen"].ToString();
                }
                if (row["add_time"] != null)
                {
                    model.Add_time = DateTime.Parse(row["add_time"].ToString());
                }
                if (row["shuxue"] != null)
                {
                    model.Shuxue = row["shuxue"].ToString();
                }
                if (row["yingyu"] != null)
                {
                    model.Yingyu = row["yingyu"].ToString();
                }
                if (row["huaxue"] != null)
                {
                    model.Huaxue = row["huaxue"].ToString();
                }
                if (row["wuli"] != null)
                {
                    model.Wuli = row["wuli"].ToString();
                }
                if (row["zhengzhi"] != null)
                {
                    model.Zhengzhi = row["zhengzhi"].ToString();
                }
                if (row["tiyu"] != null)
                {
                    model.Tiyu = row["tiyu"].ToString();
                }
                if (row["lishi"] != null)
                {
                    model.Lishi = row["lishi"].ToString();
                }
                if (row["deyu"] != null)
                {
                    model.Deyu = row["deyu"].ToString();
                }
                if (row["pingshichengji"] != null)
                {
                    model.Pingshichengji = row["pingshichengji"].ToString();
                }
                if (row["user_id"] != null)
                {
                    model.User_id = int.Parse(row["user_id"].ToString());
                }
            }
            return model;
        }
    }
}